define(function(require) {
	require("jquery");
	var appInfo = require('app/AppInfo');
	var c = require('app/utils/CommonUtils');
	var hc = require('app/utils/HttpClientUtils');
	var UserInfoUtils = require('app/utils/UserInfoUtils');
	var tip = require('app/utils/TipUtils');
	var dbu = require('app/utils/DBUtils');
	var TimerUtils = require("app/utils/TimerUtils");
	var win = window;
	var action = {};
	action.countDown = function() {
		$("#identifying_code_btn").unbind("click");
		$("#identifying_code_btn").attr("class", "aui-btn aui-btn-block aui-btn-outlined aui-btn-sm");
		var seconds = 60;
		TimerUtils.startTimer(1000, function(){
			
			seconds -= 1;
			$("#identifying_code_btn").text(seconds + "s");
			if (seconds == 0) {
				$("#identifying_code_btn").text("获取验证码");
				TimerUtils.stopTimer();
				$("#identifying_code_btn").bind("click", function() {
					action.getIdentifyingCode();
				});
				$("#identifying_code_btn").attr("class", "aui-btn aui-btn-block aui-btn-info aui-btn-outlined aui-btn-sm");
				return;
			}		
		});

	}

	action.closeWin = function() {
		api.closeToWin({
			name : 'root'
		});
	}
	action.getIdentifyingCode = function() {
		var tel = $("#tel_number").val();

		if (!c.isTel(tel)) {
			tip.show("请正确输入手机号");
			return;
		}
		var url = appInfo.serverbase + "/luyan/code_send";
		var body = {
			phoneNum : tel
		}
		hc.post(url, body, function(resp) {

			if (resp.status == 200) {
				action.countDown();
			}
		});
	}

	action.login = function() {
		var tel = $("#tel_number").val();
		var smscode = $("#identifying_code").val();
		if (!c.isTel(tel)) {
			tip.show("请正确输入手机号");
			return;
		}
		if (c.isNullStr(smscode)) {
			tip.show("验证码不能为空");
			return;
		}
		var url = appInfo.serverbase + "/luyan/login/tel/" + tel + "/smscode/" + smscode;
		hc.post(url, null, function(resp) {

			if (resp.status == 200) {
				var user = resp.result;

				if (c.isNull(user)) {
					tip.show("验证码不正确或已过期");
				} else {
					UserInfoUtils.saveUser(user);
					action.closeWin();
				}
			}
		});

	}
	action.openRegistWin = function() {
		var name = "RegistWin";
		api.openWin({
			name : name,
			url : './' + name + '.html',
			rect : {
				x : 0,
				y : 0,
				w : api.frameWidth,
				h : api.frameHeight
			},
		});
	}
	return action;
});
